package com.example.mlm;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
//import com.example.mlm.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.pojos.Product;

public class Detail_image extends Activity {
	private Product product;
	private ImageView imageView;
	private TextView titreView;
	private TextView descView;
	private ProgressBar progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_image2);
		
		imageView = (ImageView) findViewById(R.id.image);
		titreView = (TextView) findViewById(R.id.titre);
		descView = (TextView) findViewById(R.id.description);
		progress = (ProgressBar) findViewById(R.id.progress);
		
		if(product==null)
			product = (Product) getIntent().getExtras().get("product");
		imageView.setVisibility(View.INVISIBLE);
		progress.setActivated(true);
		
		titreView.setText(product.getName());
		descView.setText(product.getLongDesc());
		
		new DownloadImageTask().execute(product.getUrl());
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_detail_image, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.menu_settings:
	        	modify_pan(false);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@SuppressWarnings("unchecked")
	public void modify_pan(boolean remove){
		List<Product> panier;
		try {
            FileInputStream fStream = openFileInput("panier.bin") ;
            ObjectInputStream oStream = new ObjectInputStream(fStream);

            panier = (List<Product>) oStream.readObject();        
            oStream.close();
        } catch (Exception e) {
        	panier = new ArrayList<Product>();
            Log.e("IO Exception", e.getMessage());
        }
		if(!remove){
			if(!panier.contains(product))
				panier.add(product);
		}
		else{
			panier.remove(product);
		}
        try {
            FileOutputStream fStream = openFileOutput("panier.bin", Context.MODE_PRIVATE) ;
            ObjectOutputStream oStream = new ObjectOutputStream(fStream);

            oStream.writeObject(panier);        
            oStream.flush();
            oStream.close();

            Log.e("Serialization success", "Success panier");
        } catch (Exception e) {
            Log.e("IO Exception", e.getMessage());
        }
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelable("product", product);
	}
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		product = savedInstanceState.getParcelable("product");
	}

	private class DownloadImageTask extends AsyncTask<String, Void, Void> {
		Bitmap mIcon11 = null;
		protected Void doInBackground(String... urls) {
			String urldisplay = urls[0];
			
			try {	
				InputStream in = new java.net.URL(urldisplay).openStream();
				mIcon11 = BitmapFactory.decodeStream(in);
			    //mIcon11 = Bitmap.createScaledBitmap(mIcon11,mIcon11.getWidth() / 10, mIcon11.getHeight() / 10, true);
			} catch (Exception e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(Void result) {
			progress.setActivated(false);
			progress.setVisibility(View.INVISIBLE);
			imageView.setVisibility(View.VISIBLE);
			//imageView.set
			imageView.setImageBitmap(mIcon11);
		}

	}
}
